package cn.com.bluemoon.daps.model.controller.api;

import cn.com.bluemoon.daps.api.model.RemoteDataModelRecordService;
import cn.com.bluemoon.daps.common.constant.ServiceNameConstants;
import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.enums.BmStatus;
import cn.com.bluemoon.daps.model.entity.DapDataModelRecord;
import cn.com.bluemoon.daps.model.mapper.DapDataModelRecordMapper;
import cn.com.bluemoon.daps.model.service.DapDataModelRecordService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Optional;

/**
 * @author Jarod.Kong
 * @date 2020/12/23 17:26
 */
@Api(tags = {"FeignClient服务-模型发布记录api接口"})
@RequestMapping(ServiceNameConstants.DapsApiModel.API_MODEL_RECORD)
@BmPermission(value = "", name = "服务注册-模型发布记录", ignore = true)
@RestController
@Slf4j
public class DapDataModelRecordServiceApi implements RemoteDataModelRecordService {
    @Resource
    private DapDataModelRecordService dapDataModelRecordService;

    @GetMapping("/getMaxReleaseTime")
    public Optional<LocalDateTime> findMaxReleaseTimeBySystemId(@RequestParam("systemId") String systemId) {
        LocalDateTime maxReleaseTimeBySystemId = dapDataModelRecordService.getBaseMapper(DapDataModelRecordMapper.class).findMaxReleaseTimeBySystemId(systemId);
        return Optional.ofNullable(maxReleaseTimeBySystemId);
    }

    @GetMapping("/exists/record")
    public ResultBean<DapDataModelRecord> existsRecordByModelId(@RequestParam("modelId") String modelId) {
        DapDataModelRecord baseModel = dapDataModelRecordService.getBaseMapper(DapDataModelRecordMapper.class)
                .selectOne(new LambdaQueryWrapper<DapDataModelRecord>()
                        .isNull(DapDataModelRecord::getReleaseTime)
                        .eq(DapDataModelRecord::getModelId, modelId)
                        .eq(DapDataModelRecord::getBmStatus, BmStatus.ON.getCode())
                        .last("limit 1"));
        return ResultBean.ok(baseModel);
    }
}
